package com.etlmaster.executor.steps.log;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class ThreadMessages {
	private int size;
	public ThreadMessages(int size){
		this.size=size;
	}
	private List<Message> messages= new ArrayList<Message>();
	class Message{
		private String threadName;
		private String date;
		private String level;
		private String message;
		private String hour;
		public Message(String date,String hour,String threadName,String level,String message){
			this.setDate(date);
			this.setHour(hour);
			this.setThreadName(threadName);
			this.setMessage(message);
			this.setLevel(level);
		}
		public boolean hasError(){
			if(getLevel().contains("ERROR")){
				return true;
			}else{
				return false;
			}
		}
		public String getDate() {
			return date;
		}
		public void setDate(String date) {
			this.date = date;
		}
		public String getLevel() {
			return level;
		}
		public void setLevel(String level) {
			this.level = level;
		}
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}
		public String getHour() {
			return hour;
		}
		public void setHour(String hour) {
			this.hour = hour;
		}
		public String getThreadName() {
			return threadName;
		}
		public void setThreadName(String threadName) {
			this.threadName = threadName;
		}
	}
	public Message addMessage(String date,String hour,String threadName,String level,String message){
		Message msg=new Message(date,hour,threadName,level,message);
		if(getMessages().size()==this.size){
			getMessages().remove(0);
		}
		getMessages().add(msg);
		return msg;
	}
	public void appendMessage(String message){
		Message msg=getMessages().get(size-1);
		msg.setMessage(msg.getMessage()+"\n"+message);
	}
	public List<Message> getMessages() {
		return messages;
	}
	public void setMessages(List<Message> messages) {
		this.messages = messages;
	}

}
